package com.example.wechat;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.wechat.database.WeChatDatabaseManager;
import java.util.List;

public class MomentsActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private Button btnPublish;
    private MomentsAdapter adapter;
    private WeChatDatabaseManager dbManager;
    private int currentUserId = 1; // 假设当前用户ID为1

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_moments);
        recyclerView = findViewById(R.id.recycler_moments);
        btnPublish = findViewById(R.id.btn_publish);
        dbManager = new WeChatDatabaseManager(this);
        dbManager.open();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        loadMoments();
        btnPublish.setOnClickListener(v -> {
            startActivity(new Intent(this, PublishMomentActivity.class));
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        loadMoments();
    }

    private void loadMoments() {
        List<Moment> moments = dbManager.getAllMoments();
        if (adapter == null) {
            adapter = new MomentsAdapter(moments);
            recyclerView.setAdapter(adapter);
        } else {
            adapter.setData(moments);
        }
    }
} 